﻿using Forum.BaseController;
using Forum.Filter;
using Forum.Service.Interface;

namespace Forum.Controllers
{
    /// <summary>
    /// 推荐博客
    /// </summary>
    [Route("Api/FeaturedPost")]
    public class FeaturedPostController : BaseController<IFeaturedPostService>
    {
        private readonly IPostService _postService;
        public FeaturedPostController(IFeaturedPostService defaultService, ILogger<BaseController<IFeaturedPostService>> logger, IPostService postService) : base(defaultService, logger)
        {
            _postService = postService;
        }

        [HttpGet]
        public ApiResponse<List<FeaturedPost>> GetList()
        {
            var list = DefaultService.GetList();
            return new ApiResponse<List<FeaturedPost>>(list);
        }

        [HttpGet("{id:int}")]
        [PreventDuplicateRequests(50, "id")]
        public ApiResponse<FeaturedPost> Get(int id)
        {
            var item = DefaultService.Get(id);
            return item == null ? ApiResponse.NotFound() : new ApiResponse<FeaturedPost>(item);
        }

        [HttpPost]
        public ApiResponse<FeaturedPost> Add([FromQuery] string postId)
        {
            var post = _postService.GetById(postId);
            if (post == null) return ApiResponse.NotFound("博客不存在");
            var item = new FeaturedPost { PostId = postId };
            DefaultService.Insert(item);
            return new ApiResponse<FeaturedPost>(item);
        }

        [HttpDelete("{id:int}")]
        public ApiResponse Delete(int id)
        {
            var item = DefaultService.Get(id);
            if (item == null) return ApiResponse.NotFound("推荐博客记录不存在");
            DefaultService.Delete(item);
            return ApiResponse.OK();
        }
    }
}
